package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    /**
     * 敏感词模糊分页查询
     */
    @Override
    public ResponseResult findSensitiveList(SensitiveDto sensitiveDto) {
        //判断传入的对象是否合法
        sensitiveDto.checkParam();
        //根据分页查询
        String name = sensitiveDto.getName();
        Integer curPage = sensitiveDto.getPage();
        Integer size = sensitiveDto.getSize();
        Page page = new Page(curPage, size);
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like(AdSensitive::getSensitives, name);
        }
        IPage iPage = this.page(page, wrapper);
        long total = iPage.getTotal();
        List list = page.getRecords();
        ResponseResult result = new PageResponseResult(curPage, size, total);
        result.setData(list);
        return result;
    }

    /**
     * 敏感词删除
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        //判断数据是否合法
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断数据库中是否有该敏感词
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdSensitive::getId, id);
        AdSensitive sensitive = getOne(wrapper);
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //执行删除操作
        remove(wrapper);
        return ResponseResult.okResult();
    }

    /**
     * 新增敏感词
     */
    @Override
    public ResponseResult addSensitive(AdSensitive adSensitive) {
        String sensitiveName = adSensitive.getSensitives();
        //判断数据是否合法
        if (StringUtils.isBlank(sensitiveName)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词不能为空！！！");
        }
        //判断数据库中是否存在当前敏感词
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdSensitive::getSensitives, sensitiveName);
        AdSensitive sensitive = getOne(wrapper);
        if (sensitive != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult();
    }


    /**
     * 根据id修改
     */
    @Override
    public ResponseResult updateSensitive(AdSensitive adSensitive) {
        String sensitiveName = adSensitive.getSensitives();
        Integer sensitiveId = adSensitive.getId();
        //判断数据是否合法
        if (adSensitive == null || StringUtils.isBlank(sensitiveName)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断当前数据库中的敏感词跟要修改的敏感词是否相同
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AdSensitive::getSensitives, sensitiveName);
        AdSensitive sensitive = getOne(wrapper);
        System.out.println(sensitiveId);
        AdSensitive sensitiveById = getById(sensitiveId);
        System.out.println("sensitiveById = " + sensitiveById);
        if (sensitiveById == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if ((!sensitiveById.getSensitives().equals(sensitiveName)) && sensitive == null) {
            adSensitive.setCreatedTime(new Date());
            updateById(adSensitive);
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "当前敏感词已经存在");
    }
}
